﻿@page "/stall/edit/{Id}"
@page "/stall/edit"

@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IPopupService PopupService
@inject IPageModelCatche<StallViewModel> _pageModelCatche

<CnGalWebSite.ProjectSite.Shared.Components.Shared.TitleCard Title="@_name" />

<AuthorizeView>
    <Authorized>
        <div>
            @if (Model == null)
            {
                <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
            }
            else
            {
                <CnGalWebSite.Components.Containers.MasaTitleContainer Title="@_name" Icon="@IconType.Stall.ToIconString()" Outline>
                 <CnGalWebSite.ProjectSite.Shared.Components.Stalls.EditCards.MainCard Model="Model" OnTypeChanged="OnTypeChanged" />
                 <CnGalWebSite.ProjectSite.Shared.Components.Stalls.EditCards.ImageCard Model="Model" />
                 @if (Model.PositionType == ProjectPositionType.CV)
                    {
                        <CnGalWebSite.ProjectSite.Shared.Components.Stalls.EditCards.AudioCard Model="Model" />
                    }
                    @if (Model.PositionType == ProjectPositionType.Writer)
                    {
                        <CnGalWebSite.ProjectSite.Shared.Components.Stalls.EditCards.TextGroupCard Model="Model" />
                    }
                    <CnGalWebSite.ProjectSite.Shared.Components.Shared.EditCards.SubmitCard Model="Model" OnSubmit="OnSubmit" />
                </CnGalWebSite.Components.Containers.MasaTitleContainer>
            }

        </div>
</Authorized>
<NotAuthorized>
    <CnGalWebSite.ProjectSite.Shared.Components.Errors.NotAuthorizedCard/>
</NotAuthorized>
</AuthorizeView>

<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />

@code {
    [Parameter]
    public string Id { get; set; }

    private StallEditModel Model { get; set; }
    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;

    string _name = "创建橱窗";

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override void OnInitialized()
    {
        _name = string.IsNullOrWhiteSpace(Id) ? "创建橱窗" : "编辑橱窗";
    }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                Model = await _httpService.GetAsync<StallEditModel>($"api/stall/edit?id={Id ?? "0"}");
                StateHasChanged();
            }
            catch (Exception ex)
            {
                //await ErrorHandler.ProcessError(ex, "获取橱窗信息失败");
            }
        }
    }

    public void OnTypeChanged()
    {
        StateHasChanged();
    }

    private async Task OnSubmit()
    {
        try
        {
            Model.Identification = await deviceIdentification.GetDeviceIdentificationModel();

            var obj = await _httpService.PostAsync<StallEditModel, Result>("api/stall/edit", Model);
            //判断结果
            if (obj.Success == false)
            {
                await PopupService.ToastErrorAsync($"{_name}失败", obj.Message);
            }
            else
            {
                await PopupService.ToastSuccessAsync($"{_name}成功");
                _pageModelCatche.Clean($"api/stall/get?id={Id}");
                NavigationManager.NavigateTo("/stall/" + obj.Message);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, $"{_name}失败");
        }
    }
}
